package com.dianru.android.sdk.adwall.sample;

import java.util.Timer;
import java.util.TimerTask;

import com.dianru.adwall.sdk.AdService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MytestActivity extends Activity implements OnClickListener {
	private Button buttonAdwall;
	private Button buttonShowPoint;
	private Button buttonsendPoint;
	private Button buttonPush;
	private Button buttonYijian;
	private TextView textView;
	private Timer timer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		buttonAdwall = (Button) findViewById(R.id.button1);
		buttonShowPoint = (Button) findViewById(R.id.button2);
		buttonsendPoint = (Button) findViewById(R.id.button3);
		buttonPush = (Button) findViewById(R.id.button4);
		buttonYijian = (Button) findViewById(R.id.button5);
		textView = (TextView) findViewById(R.id.textView1);
		buttonAdwall.setOnClickListener(this);
		buttonShowPoint.setOnClickListener(this);
		buttonsendPoint.setOnClickListener(this);
		buttonPush.setOnClickListener(this);
		buttonYijian.setOnClickListener(this);
		AdService.initPoint(this);//
		AdService.showPoint(MytestActivity.this);// 
		getUIPoint();
		textView.setText("请稍后");
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			AdService.getAdWall(this);// 进入点积分列表
			break;
		case R.id.button2:
			AdService.showPoint(MytestActivity.this);// 开发中调用
			getUIPoint();  //示例中显示积分代码      
			break;
		case R.id.button3:
			/**
			 * AdService.sendPoint(MytestActivity.this,10)
			 *   return boolean isSend
			 */
			boolean  isSend= AdService.sendPoint(MytestActivity.this,10);// 开发者调用               第二个参数为用户想要消费的积分数
			if(isSend)
				Toast.makeText(MytestActivity.this, "消费成功", Toast.LENGTH_LONG).show();
			else
				Toast.makeText(MytestActivity.this, "消费失败", Toast.LENGTH_LONG).show();
			sendPoint();//示例中显示消费的状态及其结果
			break;
		case R.id.button4:
			Toast.makeText(this, "首次30秒后发送推送广告，之后每隔半小时自动下发", Toast.LENGTH_LONG)
					.show();
			break;
		case R.id.button5:
			Intent inte = new Intent(this, web.class);
			startActivity(inte);
			this.finish();
			break;
		}

	}

	// 网络延迟 数据不通畅的原因造成不能立马显示积分 要时时更新一下网络数据
	// 实际开发中不用 只是示例代码。实际开发中 积分在AdActivity中显示
	// 开发者开发的应用中 不用提供显示代码的 区域
	// 实际开发中 AdService.getInstance().showPoint() 不用去主动的调用
	private void getUIPoint() {
		if (timer != null) {
			timer.cancel();
		}
		timer = new Timer();
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				String point = AdService.showPoint(
						MytestActivity.this, true);
				textView.setText(point);
			}
		};
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				handler.sendMessage(new Message());
			}
		}, 1000, 1000);
	}

	/*
	 * 消费完成后并在页面上显示 是否消费成功， 实际开发中不会用到 实际开发中 开发中只需要调用
	 * AdService.sendPoint(MytestActivity.this, 10);
	 * 即可 该方法为示例方法
	 */
	private void sendPoint() {
		if (timer != null) {
			timer.cancel();
		}
		timer = new Timer();
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				String point = AdService.sendPoint(MytestActivity.this);// ��ѻ��
				textView.setText(point);
			}
		};
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				handler.sendMessage(new Message());
			}
		}, 0, 1000);
		getUIPoint();
	}

	@Override
	protected void onDestroy() {
		if (timer != null) {
			timer.cancel();
		}
		AdService.destroy();
		super.onDestroy();
	}

}